package br.com.gbvbahia.mybooks;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class Config extends ActivityMae {

	public static final String LOGIN_ATIVO = "loginBookAtivo";
	public static final String LOGIN = "loginBook";
	public static final String SENHA = "senhaBook";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.configlogin);

		final CheckBox chk = (CheckBox) findViewById(R.id.checkConfigAtivar);
		final EditText textLogin = (EditText) findViewById(R.id.editConfigLogin);
		final EditText textSenha = (EditText) findViewById(R.id.editConfigSenha);
		final Button btnSalvar = (Button) findViewById(R.id.btnConfigSalvar);
		final SharedPreferences preferencia = getSharedPreferences("config",
				Context.MODE_PRIVATE);
		boolean loginAtivo = preferencia.getBoolean(LOGIN_ATIVO, false);
		chk.setChecked(loginAtivo);
		
		if(loginAtivo){
			textLogin.setText(preferencia.getString(LOGIN, getString(R.string.informarLogin)));
			textSenha.setText(preferencia.getString(SENHA, ""));
		}
	
		btnSalvar.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				SharedPreferences.Editor editor = preferencia.edit();
				boolean loginIsAtivo = chk.isChecked();
				editor.putBoolean(LOGIN_ATIVO, loginIsAtivo);
				
				if (loginIsAtivo) {
					String login = textLogin.getText().toString();
					String senha = textSenha.getText().toString();
					if(TextUtils.isEmpty(login) || TextUtils.isEmpty(senha)){
						Toast.makeText(getBaseContext(),
								getString(R.string.aviso_4),
								Toast.LENGTH_SHORT).show();
						return;
					}
					editor.putString(LOGIN, login);
					editor.putString(SENHA, senha);
				}
				editor.commit();
				if (loginIsAtivo) {
					Toast.makeText(getBaseContext(),
							getString(R.string.aviso_5),
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getBaseContext(),
							getString(R.string.aviso_6),
							Toast.LENGTH_SHORT).show();
				}
				finish();
			}
		});
	}
}
